//
//  ViewController.m
//  JoinSdkDemo
//
//  Created by mac on 2021/4/8.
//

#import "ViewController.h"
#import "JoinSdkDemo-Bridging-Header.h"

@interface ViewController () <UserListener, PayListener> //实现回调代理 todo

@end



@implementation ViewController

#pragma mark -
#pragma mark 实现回调代理 todo

//登录成功返回
- (void)loginSuccess:(SdkUser * _Nullable)sdkUser {
    NSString *user = [NSString stringWithFormat:@"userId=%@ , token=%@ , appId=%@, channel=%@ 已复制", sdkUser.userId, sdkUser.token, sdkUser.appId, sdkUser.channel];
    NSLog(@"loginSuccess:%@", user);
    //复制字符串到剪切板
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setString:user];

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录成功" message:user preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

//取消登录
- (void)loginFailed {
    NSLog(@"loginFailed");
}

//登出
- (void)logout {
    NSLog(@"logout");
    //重新打开登录页面
}

//支付成功
- (void)paySuccess:(NSString *)str {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"支付成功" message:str preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

//支付失败或取消
- (void)payFail:(NSString *)str {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"支付失败或取消" message:str preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark -
#pragma mark 回调代理

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

// 登录
- (IBAction)clickLogin {
    [JoinSdk login:self];
}

- (IBAction)clickLogout {
    [JoinSdk logout:self];
}

//支付6元
- (IBAction)clickPay1 {
    PayParams * param = [self buildPayParams:600];
    [JoinSdk pay:param listener:self];
}

//支付648元
- (IBAction)clickPay2 {
    PayParams * param = [self buildPayParams:64800];
    [JoinSdk pay:param listener:self];
}

- (PayParams *)buildPayParams:(int)money {
    //商品id需要在苹果开发网站配置好,核对好价格
    NSString *productId = @"600001";
    if(money != 600) {
        productId = @"mytest111";
    }

    //todo
    PayParams *params = [[PayParams alloc] init];
    //必填的参数
    params.orderId = [self generateUuid];     //订单编号,必填
    params.productId = productId;   //商品ID,必填
    params.productName = [NSString stringWithFormat:@"%d元", money/100]; //商品名称,不为空(必填)⽀付宝要求
    params.productDesc = params.productName; //商品描述,不为空(必填)⽀付宝要求,字符数在2~到1000之间,微信⽀付要求
    params.money = money;       //支付金额,单位人民币分
    params.count = 1;           //购买数量,默认1,总付款金额money*count
    params.callBackInfo = @"透传参数%@xcv*$b%2s";  //额外参数,发往游戏服务器支付通知地址
    params.callbackUrl = @"https://open.game12.cn/api/wxPay/sdkNotify";     //游戏服务器接收支付通知地址

    //一些渠道发⾏合作关系时必填,其他选填或者忽略
    params.roleId = @"123";      //玩家角色ID
    params.roleName = @"我在玩";  //玩家角色名
    params.roleLevel = @"33";    //角色等级,要求可以转成数字
    params.zoneId = @"3";        //游戏区服ID
    params.zoneName = @"五区";    //游戏区服名称
    params.vip = @"";            //用户VIP等级
    params.balance = @"12345";   //游戏币余额,要求可以转成数字
    params.ratio = @"1";         //游戏道具和⼈民币的⽐率,1元人民币可以兑换多少个道具,要求可以转成数字

    return params;
}

- (NSString *)generateUuid {
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];
    CFRelease(uuid_ref);
    CFRelease(uuid_string_ref);
    return [uuid lowercaseString];
}

//角色上报
- (IBAction)setRole {
    //todo
    SdkRole *role = [[SdkRole alloc] init];
    role.type = SdkRole.TYPE_ENTER_GAME; //上报类型,进入服务器TYPE_ENTER_GAME,创建角色TYPE_CREATE_ROLE,玩家升级TYPE_LEVEL_UP,退出游戏TYPE_EXIT_GAME
    role.roleId = @"123";       //玩家角色ID
    role.roleName = @"我在玩";   //玩家角色名
    role.roleLevel = @"33";     //角色等级,要求可以转成数字
    role.zoneId = @"3";         //游戏区服ID
    role.zoneName = @"五区";     //游戏区服名称
    role.vip = @"";             //用户VIP等级
    role.balance = @"12345";    //游戏币余额,要求可以转成数字
    role.partyName = @"1";      //帮派公会名称
    role.extra = @"ext";        //扩展信息

    [JoinSdk setRole:role];
}

@end
